package com.xdw.vuebackdemo.controller;

import com.xdw.vuebackdemo.base.common.ApiCodeConstant;
import com.xdw.vuebackdemo.base.common.ApiResult;
import com.xdw.vuebackdemo.constant.Constant;
import com.xdw.vuebackdemo.model.User;
import com.xdw.vuebackdemo.service.UserService;
import com.xdw.vuebackdemo.util.Base64Util;
import com.xdw.vuebackdemo.util.RedisUtil;
import com.xdw.vuebackdemo.util.TokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author xiadewang
 */
@RestController
@RequestMapping("/api")
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private RedisUtil redisUtil;
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public ApiResult<Map<String,Object>> login(String username, String password, @RequestHeader(value = "client_type",required = false) String clientType) throws UnsupportedEncodingException {
        System.out.print("username="+username+",password="+password);
        //静态数据模拟
/*        if("admin".equals(username) && "123456".equals(password)){
            apiResult.setCode(200);
            apiResult.setMsg("登录成功");
        }else {
            apiResult.setCode(-1);
            apiResult.setMsg("用户名或密码错误");
        }*/
        User user = userService.login(username,password);
        if(user!=null){
            Map<String,Object> dataMap = new HashMap<>();
            dataMap.put("user",user);
            //如果是Android app发送的请求
            String tokenKey = "token:"+user.getId();

            if(Constant.CLIENT_TYPE_ANDROID.equals(clientType) || Constant.CLIENT_TYPE_IOS.equals(clientType) || Constant.CLIENT_TYPE_WECHAT.equals(clientType)){
                //redisUtil.hset(tokenKey,clientType,TokenUtil.generateTokenValue(clientType,user.getId()),TOKEN_EXPIRE_TIME);
                Map<String, Object> map = new HashMap();
                String tokenValue = TokenUtil.generateTokenValue(clientType,user.getId());
                map.put(clientType, tokenValue);
                redisUtil.hmset(tokenKey,map,Constant.TOKEN_EXPIRE_TIME);
                dataMap.put("token",Base64Util.encode(user.getId()+"-"+tokenValue));
            } else {
                //redisUtil.hset(tokenKey,Constant.CLIENT_TYPE_WEB,TokenUtil.generateTokenValue(Constant.CLIENT_TYPE_WEB,user.getId()),Constant.TOKEN_EXPIRE_TIME);
                Map<String, Object> map = new HashMap();
                String tokenValue = TokenUtil.generateTokenValue(Constant.CLIENT_TYPE_WEB,user.getId());
                map.put(Constant.CLIENT_TYPE_WEB, tokenValue);
                redisUtil.hmset(tokenKey,map,Constant.TOKEN_EXPIRE_TIME);
                //将userid和tokenValue使用"-"拼接在一起,然后采用Base64混淆之后返回给前端
                dataMap.put("token",Base64Util.encode(user.getId()+"-"+tokenValue));
            }
            return ApiResult.success(dataMap);
        } else{
            return ApiResult.failure(ApiCodeConstant.RESULT_LOGIN_FAIL,"用户名或密码错误");
        }

    }


    @RequestMapping(value = "/logined",method = RequestMethod.POST)
    public ApiResult<User> logined(){
        return ApiResult.success();
    }


}
